import time
from usart_stm32.usart import get_car_data, send_car_data
from server_api import get_work, post_order_state, has_work, post_temp_hum, post_point, post_warning, get_control
from calculate.car_operator import Operate
from calculate.car_direction import pos_to_direction, is_arrive


server = 'http://106.53.119.46'
car_id = 1
car_pt = '113.08383,28.321184'  # 从stm32获取

oid = None
i_time = None
i_temp = None
i_hum = None
warning_flag = None
complete_flag = False

control_flag = False


def get_car_control():
    global control_flag
    data = get_control(server, car_id)
    if data is None:
        return
    control_flag = True
    send_car_data(data)


def data_cleaning(int_temp, int_hum, int_warning_flag):
    if int_temp < 5 or int_temp > 50 or int_hum < 20 or int_hum > 100 or (int_warning_flag != 0 and int_warning_flag != 1 and int_warning_flag != 2):
        return True  # 有异常,需跳过
    return False  # 无异常

# 组合
if __name__ == '__main__':
    print('begin')
    # 接收任务
    while True:
        # 从stm32接收数据
        car_pt, i_temp, i_hum, warning_flag = get_car_data()  # get_car_data()未接收到数据会阻塞
        time.sleep(0.5)
        if car_pt == '0':
            continue
        try:
            # int_car_pt = int(car_pt)
            int_temp = int(i_temp)
            int_hum = int(i_hum)
            int_warning_flag = int(warning_flag)
        except:
            continue

        if data_cleaning(int_temp, int_hum, int_warning_flag):
            time.sleep(0.5)
            continue
        

        # print(f"point:{car_pt}, temp:{i_temp}, hum:{i_hum}, warning:{warning_flag}")
        # print('\n')

        car_pt = '113.08383,28.321184' # 湖南信息学院经纬度

        if not has_work(server):
            time.sleep(0.5)
            continue

        # print("开始接收订单")
        # 获取任务
        get_task_flag, oid, paths = get_work(server, car_pt, car_id, i_temp, i_hum)
        if get_task_flag == 1:
            print("接受任务成功")
            break
        elif get_task_flag == 0:
            print("暂无待配送的订单")
        else:
            print("接收任务失败")

    # print("已经接收订单完毕")
    send_car_data('o')
    while True:
        car_pt, i_temp, i_hum, warning_flag = get_car_data()
        time.sleep(0.5)
        try:
            # int_car_pt = int(car_pt)

            int_warning_flag = int(warning_flag)
            int_temp = int(i_temp)
            int_hum = int(i_hum)
        except:
            continue
        if data_cleaning(int_temp, int_hum, int_warning_flag):
            time.sleep(0.5)
            continue

        post_temp_hum(server, oid, i_temp, i_hum)
        post_point(server, oid, car_pt)
        post_warning(server, car_id, oid, warning_flag)

        # print(f"pt:{car_pt}; temp: {i_temp}; hum:{i_hum}; warning: {warning_flag}")
        # data = get_control(server, car_id)
        # print(f"data: {data}")
        # send_car_data(data)

        data_send_to = "$OK,"

        for key in paths.keys():
            for pt in paths[key]:
                data_send_to += str(pt[1]) + ',' + str(pt[0]) + ','

        data_send_to += ',n'

        send_car_data(data_send_to.encode())

        # print(f"发送完毕")
        

    

        

    






    


